package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.base.domain.BaseDevice;
import com.ruoyi.base.service.IBaseDeviceService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseFlowMapper;
import com.ruoyi.base.domain.BaseFlow;
import com.ruoyi.base.service.IBaseFlowService;

/**
 * 设备工单Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-04-15
 */
@Service
public class BaseFlowServiceImpl implements IBaseFlowService 
{
    private static final Logger log = LoggerFactory.getLogger(BaseFlowServiceImpl.class);

    @Autowired
    private BaseFlowMapper baseFlowMapper;
    @Autowired
    private IBaseDeviceService baseDeviceService;

    /**
     * 查询设备工单
     * 
     * @param id 设备工单主键
     * @return 设备工单
     */
    @Override
    public BaseFlow selectBaseFlowById(Long id)
    {
        return baseFlowMapper.selectBaseFlowById(id);
    }

    /**
     * 查询设备工单列表
     * 
     * @param baseFlow 设备工单
     * @return 设备工单
     */
    @Override
    @DataScope(deptAlias = "t", userAlias = "t")
    public List<BaseFlow> selectBaseFlowList(BaseFlow baseFlow)
    {
        return baseFlowMapper.selectBaseFlowList(baseFlow);
    }

    /**
     * 新增设备工单
     * 
     * @param baseFlow 设备工单
     * @return 结果
     */
    @Override
    public int insertBaseFlow(BaseFlow baseFlow)
    {
        if(baseFlow.getDeviceId() != null){
            BaseDevice baseDevice = baseDeviceService.selectBaseDeviceById(baseFlow.getDeviceId());
            baseFlow.setTypeId(baseDevice.getTypeId());
            baseFlow.setDeviceCode(baseDevice.getCode());
            baseFlow.setAmount(1);
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        baseFlow.setDeptId(user.getDeptId());
        baseFlow.setUserId(user.getUserId());
        baseFlow.setCreateBy(user.getUserName());
        baseFlow.setCreateTime(DateUtils.getNowDate());
        return baseFlowMapper.insertBaseFlow(baseFlow);
    }

    /**
     * 修改设备工单
     * 
     * @param baseFlow 设备工单
     * @return 结果
     */
    @Override
    public int updateBaseFlow(BaseFlow baseFlow)
    {
        baseFlow.setUpdateTime(DateUtils.getNowDate());
        return baseFlowMapper.updateBaseFlow(baseFlow);
    }

    /**
     * 批量删除设备工单
     * 
     * @param ids 需要删除的设备工单主键
     * @return 结果
     */
    @Override
    public int deleteBaseFlowByIds(Long[] ids)
    {
        return baseFlowMapper.deleteBaseFlowByIds(ids);
    }

    /**
     * 删除设备工单信息
     * 
     * @param id 设备工单主键
     * @return 结果
     */
    @Override
    public int deleteBaseFlowById(Long id)
    {
        return baseFlowMapper.deleteBaseFlowById(id);
    }


    /**
     * 审核设备工单
     *
     * @param baseFlow 设备工单
     * @return 结果
     */
    public int checkBaseFlow(BaseFlow baseFlow){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        baseFlow.setCheckUserId(user.getUserId());
        baseFlow.setCheckUserName(user.getNickName());
        if(baseFlow.getCheckStatus().equalsIgnoreCase(BaseFlow.CHECK_PASS)){
            baseFlow.setFlowStatus(BaseFlow.STATUS_DOING);
        }
        int ret = 0;
        if(baseFlow.getId() != null){
            ret = baseFlowMapper.updateBaseFlow(baseFlow);
        }
        if(baseFlow.getIds() != null && baseFlow.getIds().length > 0){
            for(Long id:baseFlow.getIds()){
                baseFlow.setId(id);
                ret += baseFlowMapper.updateBaseFlow(baseFlow);
            }
        }
        return ret;
    }
}
